<?php
namespace Admin\Controller;

use Common\Util\UploadHandler;
use Common\Controller\AdminbaseController;

class BankController extends AdminbaseController {
	
    public function bankList() {
    	$bankList = M('bank')->select();
    	
    	$this->assign('list', $bankList);
    	$this->display();
    }
    
    public function editBank() {
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$bank = M('bank')->where(array('id' => $id))->find();
    		$this->assign('item', $bank);
    		$this->display();
    	} else {
    		$data = I('post.');
    		$id = I('post.id');
    		
    		unset($data ['id']);
    		$data ['update_time'] = time();
    		
    		$result = M('bank')->where(array('id' => $id))->save($data);
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('修改银行成功');
    		} else {
    			$this->error('修改银行失败');
    		}
    	}
    }
    
    public function addBank() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		$data = I('post.');
    		
    		$data ['create_time'] = time();
    		$result = M('bank')->add($data);
    		if ($result) {
    			$operation = array();
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('添加银行成功');
    		} else {
    			$this->error('添加银行失败');
    		}
    	}
    }
    
    public function uploadBankIcon() {
    	$options = C('BANK_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Bank/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    		 
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    public function deleteBank() {
    	$id = I ('get.id');
    	$result = M('bank')->where(array('id' => $id))->save(array('status' => 1));
    	if ($result) {
    		$operation = array('operation_object_id' => $id);
    		\Think\Hook::listen('operation', $operation);
    		
    		$this->success('删除成功！');
    	} else {
    		$this->success('删除失败！');
    	}
    }
}